
package common.packet;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Sent by an authenticated user to one of the servers other than the Users Server.
 * This message contains the ticket that is used by the server to verify whether
 * this client is authorized to use its services.
 *
 * @author xissburg
 */
public class Authorization implements Packet
{
    //by noew use only username
    //private UUID ticket;

    //TEMP: username
    private String username;

    public Authorization()
    {
        this("");
    }

    public Authorization(String username) {
        this.username = username;
    }

    public Authorization(DataInputStream in) throws IOException
    {
        read(in);
    }

    public Authorization(Authorization authorization) {
        this(authorization.getUsername());
    }

    public void read(DataInputStream in) throws IOException
    {
        username = in.readUTF();
    }

    public void write(DataOutputStream out) throws IOException
    {
        out.writeUTF(getCode().name());
        out.writeUTF(username);
    }

    public Code getCode() {
        return Code.AUTHORIZATION;
    }

    public Authorization copy() {
        return new Authorization(this);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
